package day19;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class demo4 {

    public static class ListNode {
   int val;
    ListNode next = null;
    public ListNode(int val) {
      this.val = val;
    }
  }

    public static ListNode mergeKLists (ArrayList<ListNode> lists) {
        ArrayList<Integer>list=new ArrayList<>();
        for (ListNode cur:lists) {
            while (cur!=null){
                list.add(cur.val);
            }
        }
       list.sort((a,b)->a-b);
        ListNode ret=null;
        ListNode cur=ret;
        for (Integer x:list) {
            cur.next=new ListNode(x);
            cur=cur.next;
        }
        return ret.next;
    }

    public static void main(String[] args) {
ListNode head1=new ListNode(1);
        head1.next=new ListNode(2);
        ListNode head2=new ListNode(3);
        head2.next=new ListNode(4);
        ArrayList<ListNode>arr=new ArrayList<>();
        arr.add(head1);
        arr.add(head2);
        ListNode ret=mergeKLists(arr);
      while (ret!=null){
          System.out.println(ret.val);
          ret=ret.next;
      }
    }


}
